package com.atgugui.eduucenter.controller;


import com.atgugui.commonutils.JwtUtils;
import com.atgugui.commonutils.Result;
import com.atgugui.commonutils.ordervo.UcenterMemberOrder;
import com.atgugui.eduucenter.entity.UcenterMember;
import com.atgugui.eduucenter.entity.vo.RegisterVo;
import com.atgugui.eduucenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-11
 */
@Api(tags = "用户登录控制层")
@RestController
@RequestMapping("/eduucenter/ucenter-member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "用户登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody UcenterMember ucenterMember) {
        String token = ucenterMemberService.login(ucenterMember);
        return Result.ok().data("token",token);
    }

    @ApiOperation(value = "用户注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return Result.ok();
    }

    @ApiOperation(value = "根据token查询用户信息")
    @GetMapping("/getUserInfo")
    public Result getUserInfoByToken(HttpServletRequest request) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = ucenterMemberService.getById(memberIdByJwtToken);
        return Result.ok().data("member",ucenterMember);
    }

    @ApiOperation(value = "根据会员id查询会员信息")
    @GetMapping("/getMemberInfo/{memberId}")
    public UcenterMemberOrder getMemberInfo(@PathVariable String memberId) {
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(ucenterMember,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    @ApiOperation(value = "统计指定日志注册的用户数量")
    @GetMapping("/countRegister/{day}")
    public Result countRegister(@PathVariable String day) {
        int count = ucenterMemberService.countRegister(day);
        return Result.ok().data("registerCount",count);
    }
}

